package cn.pn.project.service.impl;

import cn.pn.project.mapper.base.AddressDao;
import cn.pn.project.module.base.AddressModule;
import cn.pn.project.pojo.base.Address;
import cn.pn.project.service.AddressService;
import cn.pn.project.vo.base.AddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;
    @Override
    @PostMapping("/add")
    public boolean addAddress(@RequestBody Address address) throws RuntimeException, SQLException {
        //判断是否已经有默认的地址  有就将这个地址添加成非默认地址 没有就将这个地址添加成默认地址
        Address address1=new Address();
        address1.setUserId(address.getUserId());
        address1.setIsDefaultAddress(1);
        List<AddressModule> list=addressDao.selectAddress(address1);
        if(list==null||list.size()==0){
            address.setIsDefaultAddress(1);
        }else{
            address.setIsDefaultAddress(0);
        }

        return addressDao.insertAddress(address)==1;


    }

    @Override
    @PostMapping("/change")
    public boolean changeAddress(@RequestBody  Address address) throws RuntimeException, SQLException {
            return addressDao.updateAddress(address)==1;

    }

    @Override
    @PostMapping(value={"/remove"})
    public boolean removeAddress(@RequestBody Address address) throws RuntimeException, SQLException {
        //删除地址为默认地址时将某个地址设置成默认地址
        AddressModule addressModule=addressDao.findAddressByAddressId(address.getAddressId());
        if(addressModule==null){//如果要删除的地址id不存在
            return false;
        }else if(addressModule.getIsDefaultAddress()==1){//是默认地址
            //挑选一个地址设置成默认地址
            List<AddressModule> list=addressDao.selectAddress(address);
            if(list!=null&&list.size()>0){//该用户的地址若非只有一个的话
                for(int i=0; i<list.size();i++){
                    //判断当前地址不是这个即将删除的地址
                    if(!address.getAddressId().equals(list.get(i).getAddressId())){
                        //将当前地址设置成默认地址
                        list.get(i).setIsDefaultAddress(1);
                        //执行修改
                        addressDao.updateAddress(list.get(i));
                        break;
                    }
                }
            }
            //修改即将删除的地址为非默认地址
            addressModule.setIsDefaultAddress(0);
            addressDao.updateAddress(addressModule);
        }
        return addressDao.deleteAddress(address.getAddressId())==1;


    }

    @Override
    @PostMapping("/select")
    public List<AddressModule> findAddressByUserId(@RequestBody Address address) throws RuntimeException, SQLException {
        List<AddressModule> list=addressDao.selectAddress(address);
            return list;
    }
    @Override
    @PostMapping("/defaultAddressReplace")
    public void defaultAddressReplace(AddressVo addressVo) throws RuntimeException, SQLException {
        if(addressVo.getNewDefaultAddressId().equals(addressVo.getOidDefaultAddressId())){
            return;
        }
        Address address=new Address();
        address.setAddressId(addressVo.getOidDefaultAddressId());
        address.setIsDefaultAddress(0);
        addressDao.updateAddress(address);//将旧默认地址修改为非默认地址
        address.setAddressId(addressVo.getNewDefaultAddressId());
        address.setIsDefaultAddress(1);
        addressDao.updateAddress(address);//将本次订单地址设置为默认地址
    }
}
